package com.pf.service.impl;

import com.pf.common.exception.BusinessException;
import com.pf.common.helper.AuditTaskDataHelper;
import com.pf.common.processor.ValidatorProcessor;
import com.pf.dto.AuditTaskDO;
import com.pf.dto.ErrorMsgInfo;
import com.pf.enums.ResultRespEnum;
import com.pf.service.AuditTaskService;
import com.pf.utils.ExcelValidatorUtils;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/11/27 16:04
 */
@Service
public class AuditTaskServiceImpl implements AuditTaskService {
    @Override
    public List<AuditTaskDO> validateExcelData() {

        // 模拟获取excel数据
        List<AuditTaskDO> auditTaskList = AuditTaskDataHelper.getAuditTaskList();
        List<ErrorMsgInfo> errorInfoList = Lists.newArrayList();

        ExcelValidatorUtils validatorUtils = new ExcelValidatorUtils(AuditTaskDO.class);
        // 审核准则名称，审核准则类型下拉选项
        Map<String, List<String>> optionMap = new HashMap<>();
        optionMap.put("criteriaName", AuditTaskDataHelper.getCriteriaNameList());
        optionMap.put("criteriaType", AuditTaskDataHelper.getCriteriaTypeList());
        for (int i = 0; i < auditTaskList.size(); i++) {
            AuditTaskDO auditTask = auditTaskList.get(i);
            int rowNum = i + 1;
            validatorUtils.validateExcel(rowNum, auditTask, errorInfoList, new ValidatorProcessor<AuditTaskDO>() {
                @Override
                public void validateIdColumnProcessor(StringBuilder errorMsg, AuditTaskDO entity) {
                    // 校验ID列
                    validatorUtils.validateIdColumn(errorMsg, entity, AuditTaskDataHelper.getValidTaskCodeList());
                }

                @Override
                public void afterPostProcessor(StringBuilder errorMsg, AuditTaskDO entity) {
                    // 校验审核准则名称，审核准则类型下拉选项
                    validatorUtils.validateDropDownOptionColumn(errorMsg, entity, optionMap);
                    // 迎审人，审核员
                    validatorUtils.validateAccountColumn(errorMsg, entity, AuditTaskDataHelper.getAccountMap());
                }
            });
        }

        if (CollectionUtils.isNotEmpty(errorInfoList)) {
            throw new BusinessException(ResultRespEnum.VERIFY_ERROR, errorInfoList);
        }
        return auditTaskList;
    }
}
